package cn.com.cpic.insurance.rulecheck.service;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementSetter;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.com.cpic.insurance.rulecheck.entity.RuleDefinition;

@Service
@Transactional
public class RuleDefinitionService {

	private JdbcTemplate jdbcTemplate;

	@Transactional(readOnly = true)
	public List<RuleDefinition> getRuleDefinitionList() {
		return jdbcTemplate.query("SELECT * FROM t_rc_rule_def ORDER BY c_id",
				new RowMapper<RuleDefinition>() {
					public RuleDefinition mapRow(ResultSet rs, int index)
							throws SQLException {
						RuleDefinition item = new RuleDefinition();
						item.setId(rs.getLong("c_id"));
						item.setName(rs.getString("c_name"));
						item.setRemark(rs.getString("c_rem"));
						return item;
					}
				});
	}

	@Transactional(readOnly = true)
	public RuleDefinition getRuleDefinition(final Long id) {
		return jdbcTemplate.query("SELECT * FROM t_rc_rule_def WHERE c_id=?",
				new Object[] { id }, new ResultSetExtractor<RuleDefinition>() {
					public RuleDefinition extractData(ResultSet rs)
							throws SQLException, DataAccessException {
						rs.next();
						RuleDefinition item = new RuleDefinition();
						item.setId(id);
						item.setName(rs.getString("c_name"));
						item.setRemark(rs.getString("c_rem"));
						return item;
					}
				});
	}

	public long saveRuleDefinition(final RuleDefinition item) {
		if (item.getId() == null) {
			jdbcTemplate.update(
					"INSERT INTO t_rc_rule_def VALUES(nextval('seq_id'),?,?)",
					new PreparedStatementSetter() {
						public void setValues(PreparedStatement ps)
								throws SQLException {
							ps.setString(1, item.getName());
							ps.setString(2, item.getRemark());
						}
					});
			return jdbcTemplate.queryForLong("SELECT currval('seq_id')");
		} else {
			jdbcTemplate.update(
					"UPDATE t_rc_rule_def SET c_name=?,c_rem=? WHERE c_id=?",
					new PreparedStatementSetter() {
						public void setValues(PreparedStatement ps)
								throws SQLException {
							ps.setString(1, item.getName());
							ps.setString(2, item.getRemark());
							ps.setLong(3, item.getId());
						}
					});
			return item.getId();
		}
	}

	public void deleteRuleDefinition(final Long id) {
		jdbcTemplate.update("DELETE FROM t_rc_rule_def WHERE c_id=?", id);
	}

	@Autowired
	public void setDataSource(DataSource dataSource) {
		this.jdbcTemplate = new JdbcTemplate(dataSource);
	}
}
